package com.tcm.ucenter.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.base.core.exception.ServiceException;
import com.base.core.pager.PageBean;
import com.base.genid.generator.UidGenerator;
import com.base.core.pager.PageContext;
import com.base.core.dozer.DozerConvert;
import com.tcm.ucenter.domain.dto.UserAccountLogDTO;
import com.tcm.ucenter.domain.entity.UserAccountLog;
import com.tcm.ucenter.mapper.UserAccountLogMapper;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@DubboService(version = "0.0.1-alpha")
public class UserAccountLogServiceImpl implements UserAccountLogService {

    @Autowired
    private UserAccountLogMapper userAccountLogMapper;

    @DubboReference(version = "0.0.1-beta", group = "cached-uid", check = false, retries = 3)
    private UidGenerator uidGenerator;


    /**
     * 单条记录查询
     *
     * @param id
     * @return
     */
    @Override
    public UserAccountLogDTO findById(Long id) {
        UserAccountLog userAccountLog = userAccountLogMapper.findById(id);
        UserAccountLogDTO userBalanceVo = null;
        if (ObjectUtil.isNotNull(userAccountLog)) {
            userBalanceVo = DozerConvert.copyProperties(userAccountLog, UserAccountLogDTO.class);
        }
        return userBalanceVo;
    }

    /**
     * 按时间查询日志列表
     *
     * @param userId
     * @param startTime
     * @param endTime
     * @param start
     * @param pagesize
     * @return
     */
    @Override
    public PageBean<UserAccountLogDTO> listPageByDate(Long userId, Date startTime,
                                                      Date endTime, Long start,
                                                      Long pagesize) throws ServiceException {
        if (ObjectUtil.isNull(startTime)) {
            startTime = DateUtil.date();
        }
        if (ObjectUtil.isNull(endTime)) {
            //默认三个月后时间为结束时间
            endTime = DateUtil.offsetMonth(DateUtil.date(), 3);
        }
        Long count = userAccountLogMapper.countByDate(userId, null, startTime, endTime);
        PageContext.setCurrentPage(start);
        PageContext.setPageSize(pagesize);
        PageContext.setDataCount(count);
        if (count.intValue() == 0) {
            return new PageBean<>(null);
        }

        List<UserAccountLog> userAccountLogList = userAccountLogMapper.listPageByDate(userId,
                null, startTime, endTime, PageContext.getStart(), pagesize);
        List<UserAccountLogDTO> userAccountLogDTOS =
                userAccountLogList.stream().map(userAccountLog -> {
                    UserAccountLogDTO userAccountLogDTO = new UserAccountLogDTO();
                    BeanUtils.copyProperties(userAccountLog, userAccountLogDTO);
                    return userAccountLogDTO;
                }).collect(Collectors.toList());
        PageContext.clear();
        return new PageBean<>(userAccountLogDTOS);
    }

    /**
     * 按时间段查询
     *
     * @param userId
     * @param category
     * @param startTime
     * @param endTime
     * @param start
     * @param pagesize
     * @return
     */
    @Override
    public PageBean<UserAccountLogDTO> listPageByDate(Long userId, String category, Date startTime, Date endTime,
                                                      Long start, Long pagesize) throws ServiceException {
        if (ObjectUtil.isNull(startTime)) {
            startTime = DateUtil.date();
        }
        if (ObjectUtil.isNull(endTime)) {
            //默认三个月后时间为结束时间
            endTime = DateUtil.offsetMonth(DateUtil.date(), 3);
        }
        Long count = userAccountLogMapper.countByDate(userId, category, startTime, endTime);
        PageContext.setCurrentPage(start);
        PageContext.setPageSize(pagesize);
        PageContext.setDataCount(count);
        if (count.intValue() == 0) {
            return new PageBean<>(null);
        }

        List<UserAccountLog> userAccountLogList = userAccountLogMapper.listPageByDate(userId,
                category, startTime, endTime, PageContext.getStart(), pagesize);
        List<UserAccountLogDTO> userAccountLogDTOS =
                userAccountLogList.stream().map(userAccountLog -> {
                    UserAccountLogDTO userAccountLogDTO = new UserAccountLogDTO();
                    BeanUtils.copyProperties(userAccountLog, userAccountLogDTO);
                    return userAccountLogDTO;
                }).collect(Collectors.toList());
        PageContext.clear();
        return new PageBean<>(userAccountLogDTOS);
    }

    /**
     * 分页
     *
     * @param userId
     * @param start
     * @param pagesize
     * @return
     */
    @Override
    public PageBean<UserAccountLogDTO> listPageByUserId(Long userId, Long start, Long pagesize) throws ServiceException {
        Long count = userAccountLogMapper.countByUserId(userId, null);
        PageContext.setCurrentPage(start);
        PageContext.setPageSize(pagesize);
        PageContext.setDataCount(count);
        if (count.intValue() == 0) {
            return new PageBean<>(null);
        }

        List<UserAccountLog> userAccountLogs = userAccountLogMapper.listPageByUserId(userId, null,
                PageContext.getStart(), pagesize);
        List<UserAccountLogDTO> userAccountLogDTOS = userAccountLogs.stream().map(userAccountLog -> {
            UserAccountLogDTO userAccountLogDTO = new UserAccountLogDTO();
            BeanUtils.copyProperties(userAccountLog, userAccountLogDTO);
            return userAccountLogDTO;
        }).collect(Collectors.toList());
        PageContext.clear();
        return new PageBean<>(userAccountLogDTOS);
    }

    /**
     * 分页
     *
     * @param userId
     * @param category
     * @param start
     * @param pagesize
     * @return
     */
    @Override
    public PageBean<UserAccountLogDTO> listPageByUserId(Long userId, String category,
                                                        Long start, Long pagesize) throws ServiceException {
        Long count = userAccountLogMapper.countByUserId(userId, category);
        PageContext.setCurrentPage(start);
        PageContext.setPageSize(pagesize);
        PageContext.setDataCount(count);
        if (count.intValue() == 0) {
            return new PageBean<>(null);
        }

        List<UserAccountLog> userAccountLogs = userAccountLogMapper.listPageByUserId(userId,
                category, PageContext.getStart(), pagesize);
        List<UserAccountLogDTO> userAccountLogDTOS = userAccountLogs.stream().map(userAccountLog -> {
            UserAccountLogDTO userAccountLogDTO = new UserAccountLogDTO();
            BeanUtils.copyProperties(userAccountLog, userAccountLogDTO);
            return userAccountLogDTO;
        }).collect(Collectors.toList());
        PageContext.clear();
        return new PageBean<>(userAccountLogDTOS);
    }

}
